home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / GRAPHICS / TS32 / DIBSPRIT.PAS < prev    next >
Pascal/Delphi Source File  |  1996-02-11  |  3KB  |  108 lines

  1. (*********************************************
  2. TDIBSprite->TSprite
  3.  
  4. This descendant of TSprite uses a TDIB to render
  5. itself on a TWinGPaintBox.  If the TDIB is segmented
  6. (via the FramesX, FramesY property), the sprite is
  7. properly rendered using its FrameX and FrameY values.
  8.  
  9. Properties
  10.  
  11. DIB-
  12.   The TDIB that will be used to render the sprite.
  13. FrameX,FrameY-
  14.   Controls which segment of the DIB is used for
  15.   rendering.  Valid only if the TDIB is segmented
  16.   via its FramesX and FramesY properties.
  17.  
  18. Events
  19.  
  20. Methods
  21.  
  22. CreateDIBSprite-
  23.   A constructor that takes a TDIB as a parameter.
  24. Render-
  25.   The DIBCanvas of the DIB is used to render the
  26.   sprite.
  27. *********************************************)
  28.  
  29. unit Dibsprite;
  30.  
  31. interface
  32.  
  33. uses
  34.   Windows, SysUtils, Classes, Graphics, Controls, DIBDrawingSurface, Sprite, DIB,
  35.   Grafix;
  36.  
  37. type
  38.  
  39.   TDIBSprite = class( TSprite )
  40.   private
  41.      FFrameX: integer;
  42.      FFrameY: integer;
  43.      bFrames: boolean;
  44.      Width2, Height2: integer;
  45.   protected
  46.      FDIB: TDIB;
  47.   public
  48.      constructor CreateDIBSprite( dib: TDIB );
  49.      procedure Render; override;
  50.      property DIB: TDIB read FDIB write FDIB;
  51.      property FrameX: integer read FFrameX write FFrameX;
  52.      property FrameY: integer read FFrameY write FFrameY;
  53.   end;
  54.  
  55. implementation
  56.  
  57. (***************************************************
  58. Create a TDIBSprite from an existing TDIB, and assign
  59. values to variables to be used later.
  60. ***************************************************)
  61. constructor TDIBSprite.CreateDIBSprite( dib: TDIB );
  62. begin
  63.   inherited Create;
  64.   FDIB := dib;
  65.   bFrames := ( dib.FramesX > 0 ) and ( dib.FramesY > 0 );
  66.   if bFrames then
  67.      begin
  68.         Width := dib.DIBCanvas.Width div dib.FramesX;
  69.         Height := dib.DIBCanvas.Height div dib.FramesY;
  70.      end
  71.   else
  72.      begin
  73.         Width := dib.DIBCanvas.Width;
  74.         Height := dib.DIBCanvas.Height;
  75.      end;
  76.   Width2 := Width div 2;
  77.   Height2 := Height div 2;
  78.  
  79. end;
  80.  
  81. (***************************************************
  82. Render the sprite by copying a rectangle from the
  83. DIBCanvas of the TDIB to the DIBCanvas of the TDIBDrawingSurface.
  84. If the TDIB is segmented, copy only the appropriate area.
  85. ***************************************************)
  86. procedure TDIBSprite.Render;
  87. var
  88.   rectDest, rectSrc: TRect;
  89. begin
  90.   inherited Render;
  91.   if Visible then
  92.      begin
  93.         rectSrc := Rect( FrameX * Width,
  94.            FrameY * Height,
  95.            FrameX * Width + Width - 1,
  96.            FrameY * Height + Height - 1 );
  97.  
  98.         rectDest := Rect( ptPhysical.X - Width2,
  99.            ptPhysical.Y - Height2,
  100.            ptPhysical.X + Width2 - 1,
  101.            ptPhysical.Y + Height2 - 1 );
  102.  
  103.         dds.DIBCanvas.CopyRect( rectDest, dib.DIBCanvas, rectSrc );
  104.      end;
  105. end;
  106.  
  107. end.
  108.